package cn.xuchunh.tiq.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import cn.xuchunh.tiq.R;

/**
 * Created by XuChunH on 2016/4/18.
 */
public class ResultFragment extends Fragment {

    private String content;
    private ProgressBar progressBar;

    public static ResultFragment newInstance(String content) {
        Bundle args = new Bundle();
        args.putString("content", content);
        ResultFragment fragment = new ResultFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            content = args.getString("content");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_result, container, false);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        if (!TextUtils.isEmpty(content)) {
            progressBar = (ProgressBar) view.findViewById(R.id.progress);
            WebView webView = (WebView) view.findViewById(R.id.webView);
            WebSettings webSettings = webView.getSettings();
            webSettings.setBuiltInZoomControls(true);
            webSettings.setJavaScriptEnabled(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setUseWideViewPort(true);
            webView.setWebChromeClient(client);
            webView.loadDataWithBaseURL("http://m.join.qq.com", content, null, "utf-8", null);
        }
    }

    private WebChromeClient client = new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if(newProgress == 100){
                progressBar.setVisibility(View.GONE);
            }else {
                progressBar.setProgress(newProgress);
            }
        }
    };

}
